package com.djl.sqtg.controller;

import com.djl.sqtg.entity.*;
import com.djl.sqtg.service.IColonelService;
import com.djl.sqtg.util.DateUtil;
import com.djl.sqtg.util.JsonResult;
import com.alibaba.fastjson.JSONObject;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("colonel")
public class ColonelController extends BaseController {
    @Autowired
    private IColonelService colonelService;

    @PostMapping("login")
    public JsonResult login(@RequestBody JSONObject data) {
        String account = data.getString("account");
        String password = data.getString("password");
        System.out.println("登录账号："+account+",密码："+password);
        Integer auditState = colonelService.getState(account,password);
        System.out.println("state:"+auditState);
        String msg;
        if (auditState==null){
            System.out.println("账号密码错误");
            msg="账号密码错误";
            return new JsonResult(NO,msg,auditState);
        }else{
            if (auditState==1){
                System.out.println("登录成功");
                msg="登录成功";
                Colonel colonel = colonelService.login(account, password);
                return new JsonResult(OK,msg,colonel);
            }else if (auditState==0){
                System.out.println("待审核");
                msg="待审核";
                return new JsonResult(NO,msg,auditState);
            }else if (auditState==2){
                System.out.println("审核未通过");
                msg="审核未通过";
                return new JsonResult(NO,msg,auditState);
            }
        }
        return new JsonResult(OK);
    }

    @PostMapping("register")
    public JsonResult register(@RequestBody Colonel colonel) {
        System.out.println(colonel.toString());
        Colonel data = colonelService.findColonelByTelep(colonel.getTelep());
        String msg;
        if (data!=null){
            System.out.println("该手机号已经被注册");
             msg = "该手机号已经被注册";
            return new JsonResult(NO,msg);
        }
        else {
            colonel.setApplicationtime(DateUtil.getNowTime());
            colonel.setAvatar("https://tse4-mm.cn.bing.net/th/id/OIP-C.bg5x0OTgJaE7c1nsbPobaAAAAA?w=158&h=158&c=7&r=0&o=5&dpr=1.1&pid=1.7");
            colonelService.register(colonel);
             msg = "申请成功";
        }
        return new JsonResult(OK,msg);
    }

    @PostMapping("application")
    public JsonResult application(@RequestBody Colonel colonel) {
        System.out.println(colonel.toString());
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();      // 获取当前时间
        String applicationtimeStr = sdfTime.format(date);
        colonel.setApplicationtime(applicationtimeStr);
        System.out.println(colonel.toString());
        colonelService.register(colonel);
        return new JsonResult(OK);
    }

    @GetMapping("getApplicationColonel")
    public JsonResult<List<Colonel>> getApplicationColonel() {
        List<Colonel> data = colonelService.getApplicationColonel();
        return new JsonResult<List<Colonel>>(OK,data);
    }

    @RequestMapping("getApplicationColonelByPageNum/{state}")
    @ResponseBody
    public LayuiResultData getApplicationColonelByPageNum(@PathVariable Integer state,
                                                          @RequestParam(defaultValue = "1") Integer pageNum,
                                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                                          String dateSelect,
                                                          String username,
                                                          String telep,String stateSelect
                                                          ) {
        System.out.println("进入商品页面函数————————当前页码"+pageNum+"—————————————当前每页数量"+pageSize+"--------state:"+state);
        System.out.println("pagenum："+pageNum);
        System.out.println("pageSize："+pageSize);
        System.out.println("dateSelect："+dateSelect);
        System.out.println("username："+username);
        System.out.println("telep："+telep);
        System.out.println("stateSelect："+stateSelect);
        System.out.println("state："+state);
        List<Colonel> data;
        Integer count;
        if ((dateSelect==null && username==null && telep==null && stateSelect==null) || (dateSelect=="" && username=="" && telep=="" && stateSelect=="")){
            System.out.println("总数据......");
            data = colonelService.getColonelByPageNum((pageNum-1)*10,pageSize,state);
            count = colonelService.getAllColonelNum(state);
        }
        else{
            System.out.println("search..........");
            Integer stateSelect0;
            if (stateSelect=="" || stateSelect==null){
                stateSelect0=null;
            }else {
                stateSelect0=Integer.valueOf(stateSelect);
            }
            data = colonelService.getColonelBySearch(stateSelect0,dateSelect,username,telep,(pageNum-1)*10,pageSize,state);
            count = colonelService.getColonelBySearchNum(stateSelect0,dateSelect,username,telep,state);
        }
        System.out.println("分页查询结果："+data);
        System.out.println("查询数据数量"+count);
        LayuiResultData layuiResultData=new LayuiResultData();
        layuiResultData.setData(data);
        layuiResultData.setCount(count);
        layuiResultData.setPageNum(pageNum);
        layuiResultData.setPageSize(pageSize);
        return  layuiResultData;
    }

    @GetMapping("updateApplicationState/{cid}/{state}")
    public JsonResult updateApplicationState(@PathVariable Integer cid,@PathVariable Integer state) {
        System.out.println("cid"+cid);
        System.out.println("state"+state);
         colonelService.updateApplicationState(cid,state);
        return new JsonResult(OK);
    }


    @GetMapping("updateRoyalty/{cid}/{royalty}")
    public JsonResult updateRoyalty(@PathVariable Integer cid,@PathVariable Integer royalty) {
        System.out.println("cid"+cid);
        System.out.println("royalty"+royalty);
        colonelService.updateRoyalty(cid,royalty);
        return new JsonResult(OK);
    }
    @GetMapping("getMonthTurnover/{tzid}")
    public JSONObject getMonthTurnover(@PathVariable Integer tzid, String dateSelect) {
        System.out.println("tzid:"+tzid);
        System.out.println("dateSelect:"+dateSelect);
        Float turnover;
        JSONObject data = new JSONObject();
        if (dateSelect==null){//首次进入页面，即获取今月的营业额等数据
            System.out.println("首次进入页面，即获取今月的营业额等数据");
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            int day = cal.get(Calendar.DAY_OF_MONTH);
            String monthstr=year+"-0"+month;
            System.out.println("今月：" + monthstr);
             turnover=colonelService.getMonthTurnover(monthstr,tzid);
             data.put("month",monthstr);
             data.put("turnover",turnover);
        }else {
            System.out.println("选择月份查看");
            turnover=colonelService.getMonthTurnover(dateSelect,tzid);
            data.put("month",dateSelect);
            data.put("turnover",turnover);
        }
        data.put("money",000);
        data.put("royalty",10);
        List datas = new ArrayList<>();
        datas.add(data);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",1);
        jsonObject.put("data",datas);
        System.out.println(jsonObject.toString());
        return jsonObject;
    }

    @GetMapping("getTzMonthData/{tzid}")
    public JSONObject getTzMonthData(@PathVariable Integer tzid,String dateSelect) {
        System.out.println("tzid:"+tzid);
        System.out.println("dateSelect:"+dateSelect);
        List<TzMonthData> data;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        if (dateSelect==null) {//首次进入页面，即获取所有月份的数据情况
            data = colonelService.getTzMonthData(tzid);
            jsonObject.put("count",data.size());
            jsonObject.put("data",data);
            System.out.println(jsonObject.toString());
        }else {
            data =  colonelService.getTzMonthDataByDate(tzid,dateSelect);
            jsonObject.put("count",data.size());
            jsonObject.put("data",data);
            System.out.println(jsonObject.toString());
        }
        return jsonObject;
    }
//修改团长信息
    @PostMapping("updateColonel")
    public JsonResult updateColonel(@RequestBody Colonel colonel) {
        System.out.println(colonel.toString());
        colonelService.updateColonel(colonel);
        return  new JsonResult(OK);
    }
//获取团长属下的团员
    @GetMapping("getUserByTzid/{tzid}")
    public JsonResult getUserByTzid(@PathVariable Integer tzid) {
        System.out.println(tzid);
        List<User> data = colonelService.getUserByTzid(tzid);
        return  new JsonResult<>(OK,data);
    }

    //获取团长今日订单数和销售额
    @GetMapping("getColonelOrderData/{tzid}")
    public JsonResult getOrderData(@PathVariable Integer tzid) {
        System.out.println(tzid);
    //获取今日订单数
         Integer orderNum=colonelService.getColonelOrderNum(tzid,DateUtil.getToday());
    // 获取今日销售额
         Float orderMoney=colonelService.getColonelOrderMoney(tzid,DateUtil.getToday());
        if (orderMoney==null) orderMoney= Float.valueOf(0);
    // 获取累计订单总数
        Integer orderAllNum=colonelService.getColonelAllOrderNum(tzid);
    // 获取累计销售总额
        Float orderAllMoney=colonelService.getColonelOrderAllMoney(tzid);
        if (orderAllMoney==null) orderAllMoney= Float.valueOf(0);

        System.out.println("昨日日期："+DateUtil.getPastDate(1,DateUtil.getTodayDate()));
        //获取今日订单数
        Integer yesterdayOrderNum=colonelService.getColonelOrderNum(tzid,DateUtil.getPastDate(1,DateUtil.getTodayDate()));
        // 获取今日销售额
        Float yesterdayOrderMoney=colonelService.getColonelOrderMoney(tzid,DateUtil.getPastDate(1,DateUtil.getTodayDate()));
        if (yesterdayOrderMoney==null) yesterdayOrderMoney= Float.valueOf(0);

        JSONObject data = new JSONObject();
        data.put("orderNum",orderNum);
        data.put("orderMoney",orderMoney);
        data.put("orderAllNum",orderAllNum);
        data.put("orderAllMoney",orderAllMoney);
        data.put("yesterdayOrderNum",yesterdayOrderNum);
        data.put("yesterdayOrderMoney",yesterdayOrderMoney);
        return  new JsonResult(OK,data);
    }

//获取团长经营数据
    @GetMapping("getColonelBusinessData/{tzid}")
    public JsonResult getColonelBusinessData(@PathVariable Integer tzid) {
        System.out.println(tzid);
        //获取今日订单数
        Integer orderNum=colonelService.getColonelOrderNum(tzid,DateUtil.getToday());
        //获取团长提成
        Integer royalty=colonelService.getRoyalty(tzid);
        System.out.println("提成率："+royalty);
        // 获取今日销售额
        Float orderMoney=colonelService.getColonelOrderMoney(tzid,DateUtil.getToday());
        if (orderMoney==null) orderMoney= Float.valueOf(0);
        System.out.println("订单今日销售额："+orderMoney);
        //今日收益=今日销售额*提成率
        Float todayIncome = orderMoney*royalty/100;
        BigDecimal bigDecimal1 = new BigDecimal(todayIncome);
        double todayIncome0 = bigDecimal1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        // 获取累计订单总数
        Integer orderAllNum=colonelService.getColonelAllOrderNum(tzid);
        // 获取累计销售总额
        Float orderAllMoney=colonelService.getColonelOrderAllMoney(tzid);
        if (orderAllMoney==null) orderAllMoney= Float.valueOf(0);
        System.out.println("订单总销售额："+orderAllMoney);
        //累计收益=累计销售额*提成率
        Float allIncome = orderAllMoney*royalty/100;
        BigDecimal bigDecimal = new BigDecimal(allIncome);
        double allIncome0 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

//        System.out.println("昨日日期："+DateUtil.getPastDate(1,DateUtil.getTodayDate()));
        //获取昨日订单数
        Integer yesterdayOrderNum=colonelService.getColonelOrderNum(tzid,DateUtil.getPastDate(1,DateUtil.getTodayDate()));
        // 获取昨日销售额
        Float yesterdayOrderMoney=colonelService.getColonelOrderMoney(tzid,DateUtil.getPastDate(1,DateUtil.getTodayDate()));
        if (yesterdayOrderMoney==null) yesterdayOrderMoney= Float.valueOf(0);
        System.out.println("订单昨日销售额："+yesterdayOrderMoney);
        //昨日收益=昨日销售额*提成率
        Float yesterdayIncome = yesterdayOrderMoney*royalty/100;
        BigDecimal bigDecimal0 = new BigDecimal(yesterdayIncome);
        double yesterdayIncome0 = bigDecimal0.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        //用户总数
        Integer allUserNum = colonelService.getAllUserColonel(tzid);
        //本月新用户
        Integer monthyUserNum = colonelService.getMonNewUserColonel(tzid);

        //消费人数
        List consumeUserNumList = colonelService.consumeUserNum(tzid);
        Integer consumeUserNum = consumeUserNumList.size();


        JSONObject data = new JSONObject();
        data.put("allIncome",allIncome0);
        data.put("orderAllNum",orderAllNum);
        data.put("consumeUserNum",consumeUserNum);
        data.put("allUserNum",allUserNum);
        data.put("monthyUserNum",monthyUserNum);

        data.put("todayOrderNum",orderNum);
        data.put("todayIncome",todayIncome0);
        data.put("yesterdayOrderNum",yesterdayOrderNum);
        data.put("yesterdayIncome",yesterdayIncome0);
        return  new JsonResult(OK,data);
    }

    //团长提现
    @GetMapping("colonelWithdrawals/{tzid}/{money}")
    public JsonResult colonelWithdrawals(@PathVariable Integer tzid,@PathVariable Float money) {
        System.out.println(tzid);
        System.out.println(money);
        //数据库中团长收益减去前台所提现金额
        Float income = colonelService.getColonelMoney(tzid);
        System.out.println(income);
        colonelService.updateColonelMoney(tzid,income-money);
        System.out.println(income-money);
        return  new JsonResult<>(OK,income-money);
    }

}
